function cko_print(s)
  if s then
    DEFAULT_CHAT_FRAME:AddMessage(s.."\n")
  end
end

function cko_macro_create(name,icon,body)
  local idx=GetMacroIndexByName(name)
  if icon==0 then
    if cko_macro_icon==0 then
      cko_macro_icon=1
    end
    icon = cko_macro_icon
    cko_macro_icon = cko_macro_icon + 1
  end
  while idx>0 do
    cko_print("Deleting macro # "..idx)
    DeleteMacro(idx)
    idx=GetMacroIndexByName(name)
  end
  CreateMacro(name,icon,body,1,0)
end

function cko_on_event()
  if (event=='TIME_PLAYED_MSG') then
    cko_on_time_played_msg(arg1,arg2)
  end
end

function cko_on_time_played_msg(ttl,atthislevel)
  cko_print("Average seconds per kill: "..ttl/cko_cumulative_kills())
end

function cko_slash(cmd)
  a,_,x = string.find(cmd,'farm (.+)')
  if a then
    cko_farm(x)
  elseif (cmd=='zone') then
    cko_zone_guide()
  elseif (cmd=='quest') then
    cko_quest_guide()
  elseif (cmd=='level') then
    cko_xp_analysis()
  elseif (cmd=='grind xp') then
    cko_xp_grind()
  elseif (cmd=='cleanup') then
    cko_cleanup()
  elseif (cmd=='sell') then
    cko_sell()
  elseif (cmd=='precision') then
    cko_precision = 1 - cko_precision
  else
    cko_print("Unknown command: "..cmd)
  end
end

function cko_macro()
  cko_macro_create('Reload',0,'/console reloadui')
  cko_buff_macro()
  cko_cleanup_macro()
  cko_gps_macro()
  cko_inventory_macro()
  cko_xp_macro()
  cko_grind_macro()
end
